package com.hjm.common.api.ls.leshua;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ClassUtils {


    /***
     * @Description 排序实体内类的属性
     * @param clazz
     * @param excludeFieldName 不参与排序的属性
     * @Return java.util.List<java.lang.String>
     * @Author will
     * @Date 2020/11/11 20:39
     */
    public static List<String> sortedField(Class clazz, String... excludeFieldName) {
        if (clazz == null) {
            return null;
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        List<String> sortFieldList = Arrays.stream(declaredFields)
                .filter(f -> !StringUtils.containsAny(f.getName(), excludeFieldName))
                .sorted(Comparator.comparing(Field::getName))
                .map(Field::getName)
                .collect(Collectors.toList());
        return sortFieldList;
    }

    /**
     * @param obj
     * @param excludeFieldName
     * @Description 获取keyvalue 键值对
     * @Return java.lang.String
     * @Author will
     * @Date 2020/11/11 21:02
     */
    public static String getKeyValue(Object obj, String... excludeFieldName) {
        List<String> sortedField = sortedField(obj.getClass(), excludeFieldName);
        Method[] declaredMethods = obj.getClass().getDeclaredMethods();
        Map<String, Method> methodMap = Arrays.stream(declaredMethods)
                .collect(Collectors.toMap(method -> method.getName().toLowerCase(), method -> method));
        StringBuffer result = new StringBuffer();
        for (String field : sortedField) {
            //获取get方法
            String methodName = "get" + field.toLowerCase();
            try {
                Object o = methodMap.get(methodName).invoke(obj);
                //只拼接有值的部分
                if(o != null) {
                    if(o instanceof String[]){
                        String[] value = (String[]) o;
                        result.append(field).append("=").append(String.join(",",value)).append("&");
                    }else {
                        result.append(field).append("=").append(o).append("&");
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        result = result.deleteCharAt(result.length() - 1);
        return result.toString();
    }

    public static void main(String[] args) {
//        AgentSettleInfo agentSettleInfo = new AgentSettleInfo();
//        agentSettleInfo.setBankAccountNo("123123");
//        agentSettleInfo.setBankAccountType("234");
//        agentSettleInfo.setBankCardHolder("234234");
//        System.out.println(getKeyValue(agentSettleInfo,"bankCardHolder"));
    }


}
